package com.mooc.ppjoke.ui.detail;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Looper;
import android.os.UserManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.fragment.app.DialogFragment;
import androidx.lifecycle.Observer;

import com.mooc.lib_common.FileUploadManager;
import com.mooc.lib_common.FileUtils;
import com.mooc.lib_common.LoadingDialog;
import com.mooc.lib_common.MyApplication;
import com.mooc.lib_common.PixUtils;
import com.mooc.lib_common.ViewHelper;
import com.mooc.lib_network.ApiResponse;
import com.mooc.lib_network.ApiService;
import com.mooc.lib_network.JsonCallBack;
import com.mooc.ppjoke.R;
import com.mooc.ppjoke.data.Comment;
import com.mooc.ppjoke.databinding.LayoutCommentDialogBinding;
import com.mooc.ppjoke.ui.login.QQUserManager;
import com.mooc.ppjoke.ui.publish.CaptureActivity;

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName: CommentDialog$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/29 14:35
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class CommentDialog extends AppCompatDialogFragment implements View.OnClickListener{
private LayoutCommentDialogBinding mBinding;
private long itemId;
private commentAddListener mListener;
private static final String KEY_ITEM_ID="key_item_id";
private String filePath;
private int width,height;
private boolean isVideo;
private String coverUrl;
private String fileUrl;
private LoadingDialog loadingDialog;

public static CommentDialog newInstance(long itemId){

        Bundle args=new Bundle();
        args.putLong(KEY_ITEM_ID,itemId);
        CommentDialog fragment=new CommentDialog();
        fragment.setArguments(args);
        return fragment;
        }

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState){
        Window window=getDialog().getWindow();
        window.setWindowAnimations(0);

        mBinding=LayoutCommentDialogBinding.inflate(inflater,((ViewGroup)window.findViewById(android.R.id.content)),false);
        mBinding.commentVideo.setOnClickListener(this);
        mBinding.commentDelete.setOnClickListener(this);
        mBinding.commentSend.setOnClickListener(this);

        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);

        this.itemId=getArguments().getLong(KEY_ITEM_ID);

        ViewHelper.SetViewOutLine(mBinding.getRoot(), PixUtils.dp2px(10),ViewHelper.RADIUS_TOP);

        mBinding.getRoot().post(()->showSoftInputMethod());

        dismissWhenPressBack();
        return mBinding.getRoot();
        }

private void dismissWhenPressBack(){
        mBinding.inputView.setOnBackKeyEventListener(()->{
        mBinding.inputView.postDelayed(new Runnable(){
@Override
public void run(){
        dismiss();
        }
        },200);
        return true;
        });
        }


private void showSoftInputMethod(){
        mBinding.inputView.setFocusable(true);
        mBinding.inputView.setFocusableInTouchMode(true);
        //请求获得焦点
        mBinding.inputView.requestFocus();
        InputMethodManager manager=(InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.showSoftInput(mBinding.inputView,0);
        }

@Override
public void onClick(View v){
        if(v.getId()== R.id.comment_send){
        publishComment();
        }else if(v.getId()==R.id.comment_video){
        CaptureActivity.startActivityForResult(getActivity());
        }else if(v.getId()==R.id.comment_delete){
        filePath=null;
        isVideo=false;
        width=0;
        height=0;
        mBinding.commentCover.setImageDrawable(null);
        mBinding.commentExtLayout.setVisibility(View.GONE);

        mBinding.commentVideo.setEnabled(true);
        mBinding.commentVideo.setAlpha(255);
        }

        }

@Override
public void onActivityResult(int requestCode,int resultCode,@Nullable Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode== CaptureActivity.REQ_CAPTURE&&resultCode== Activity.RESULT_OK){
        filePath=data.getStringExtra(CaptureActivity.RESULT_FILE_PATH);
        width=data.getIntExtra(CaptureActivity.RESULT_FILE_WIDTH,0);
        height=data.getIntExtra(CaptureActivity.RESULT_FILE_HEIGHT,0);
        isVideo=data.getBooleanExtra(CaptureActivity.RESULT_FILE_TYPE,false);

        if(!TextUtils.isEmpty(filePath)){
        mBinding.commentExtLayout.setVisibility(View.VISIBLE);
        mBinding.commentCover.setImageUrl(filePath);
        if(isVideo){
        mBinding.commentIconVideo.setVisibility(View.VISIBLE);
        }
        }

        mBinding.commentVideo.setEnabled(false);
        mBinding.commentVideo.setAlpha(80);
        }
        }

private void publishComment(){

        if(TextUtils.isEmpty(mBinding.inputView.getText())){
        return;
        }

        if(isVideo&&!TextUtils.isEmpty(filePath)){
        FileUtils.generateVideoCover(filePath).observe(this,new Observer<String>(){
@Override
public void onChanged(String coverPath){
        uploadFile(coverPath,filePath);
        }
        });
        }else if(!TextUtils.isEmpty(filePath)){
        uploadFile(null,filePath);
        }else{
        publish();
        }
        }

@SuppressLint("RestrictedApi")
private void uploadFile(String coverPath, String filePath){
        //AtomicInteger, CountDownLatch, CyclicBarrier
        showLoadingDialog();
        AtomicInteger count=new AtomicInteger(1);
        if(!TextUtils.isEmpty(coverPath)){
        count.set(2);
        ArchTaskExecutor.getIOThreadExecutor().execute(new Runnable(){
@Override
public void run(){
        int remain=count.decrementAndGet();
        coverUrl=FileUploadManager.upload(coverPath);
        if(remain<=0){
        if(!TextUtils.isEmpty(fileUrl)&&!TextUtils.isEmpty(coverUrl)){
        publish();
        }else{
        dismissLoadingDialog();
        showToast("文件上传失败，请重新上传");
        }
        }
        }
        });
        }
        ArchTaskExecutor.getIOThreadExecutor().execute(new Runnable(){
@Override
public void run(){
        int remain=count.decrementAndGet();
        fileUrl= FileUploadManager.upload(filePath);
        if(remain<=0){
        if(!TextUtils.isEmpty(fileUrl)||!TextUtils.isEmpty(coverPath)&&!TextUtils.isEmpty(coverUrl)){
        publish();
        }else{
        dismissLoadingDialog();
        showToast("文件上传失败，请重新上传");
        }
        }
        }
        });

        }

private void publish(){
        String commentText=mBinding.inputView.getText().toString();
        ApiService.post("/comment/addComment")
        .addParam("userId", QQUserManager.getInstance().getUserId())
        .addParam("itemId",itemId)
        .addParam("commentText",commentText)
        .addParam("image_url",isVideo?coverUrl:fileUrl)
        .addParam("video_url",isVideo?fileUrl:null)
        .addParam("width",width)
        .addParam("height",height)
        .excute(new JsonCallBack<Comment>(){
@Override
public void OnSuccess(ApiResponse<Comment> response){
        onCommentSuccess(response.body);
        dismissLoadingDialog();
        }

@Override
public void OnError(ApiResponse<Comment> response){
        showToast("评论失败:"+response.message);
        dismissLoadingDialog();
        }
        });
        }

private void showLoadingDialog(){
        if(loadingDialog==null){
        loadingDialog=new LoadingDialog(getContext());
        loadingDialog.setLoadingText("正在发布...");
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setCancelable(false);
        }
        if(!loadingDialog.isShowing()){
        loadingDialog.show();
        }
        }

@SuppressLint("RestrictedApi")
private void dismissLoadingDialog(){
        if(loadingDialog!=null){
        //dismissLoadingDialog  的调用可能会出现在异步线程调用
        if(Looper.myLooper()==Looper.getMainLooper()){
        ArchTaskExecutor.getMainThreadExecutor().execute(()->{
        if(loadingDialog!=null&&loadingDialog.isShowing()){
        loadingDialog.dismiss();
        }
        });
        }else if(loadingDialog.isShowing()){
        loadingDialog.dismiss();
        }
        }
        }

@SuppressLint("RestrictedApi")
private void onCommentSuccess(Comment body){
        showToast("评论发布成功");
        ArchTaskExecutor.getMainThreadExecutor().execute(()->{
        if(mListener!=null){
        mListener.onAddComment(body);
        }
        dismiss();
        });
        }

@Override
public void dismiss(){
        super.dismiss();
        dismissLoadingDialog();
        filePath=null;
        fileUrl=null;
        coverUrl=null;
        isVideo=false;
        width=0;
        height=0;
        }

@SuppressLint("RestrictedApi")
private void showToast(String s){
        //showToast几个可能会出现在异步线程调用
        if(Looper.myLooper()==Looper.getMainLooper()){
        Toast.makeText(MyApplication.getsApplication(),s,Toast.LENGTH_SHORT).show();
        }else{
        ArchTaskExecutor.getMainThreadExecutor().execute(()->Toast.makeText(MyApplication.getsApplication(),s,Toast.LENGTH_SHORT).show());
        }
        }

public interface commentAddListener {
    void onAddComment(Comment comment);

}

    public void setCommentAddListener(commentAddListener listener) {

        mListener = listener;
    }
}
